/*
 *  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.box.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.box.dao.BoxItemDAO;
import com.pitop.box.entity.BoxItem;
import com.pitop.box.entity.BoxItemState;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.BoxException;
import com.pitop.core.exceptions.BoxItemException;
import com.pitop.sample.dao.SampleDAO;
import com.pitop.sample.entity.Sample;
import com.pitop.test.dao.TestUnitDAO;
import com.pitop.test.entity.TestUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("boxItemSV")
@Slf4j
public class BoxItemSVImpl extends BaseSVImpl<BoxItem, Long> implements BoxItemSV {

    @Autowired
    private BoxItemDAO boxItemDAO;

    @Autowired
    private TestUnitDAO testUnitDAO;

    @Autowired
    private SampleDAO sampleDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return boxItemDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(BoxItem entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象BoxItem
     *
     * @param id
     * @return BoxItem
     */
    @Override
    public BoxItem load(Long id) {
        if (id == null) {
            throw new BoxItemException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return boxItemDAO.load(param);
    }

    /**
     * 加载一个对象BoxItem 通过id
     *
     * @param id
     * @return BoxItem
     */
    @Override
    public BoxItem loadById(Long id) {
        if (id == null) {
            throw new BoxItemException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return boxItemDAO.loadById(id);
    }


    /**
     * 根据主键id,oldStates 共同更新 BoxItem 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, BoxItemState newState, BoxItemState... oldStates) {
        if (id == null) {
            throw new BoxItemException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new BoxItemException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new BoxItemException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        boxItemDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 BoxItem 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, BoxItemState state) {
        if (id == null) {
            throw new BoxItemException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        boxItemDAO.updateById(id, state.name(), new Date());
    }


    /**
     * 删除对象BoxItem
     *
     * @param id
     * @return BoxItem
     */
    @Override
    public void delete(Long id) {
        if (id == null) {
            throw new BoxItemException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        boxItemDAO.delete(param);
    }


    /**
     * 查询BoxItem分页
     *
     * @param boxItem 对象
     * @param offset  查询开始行
     * @param limit   查询行数
     * @return List<BoxItem>
     */
    @Override
    public List<BoxItem> list(BoxItem boxItem, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (boxItem != null) {
            map = JSON.parseObject(JSON.toJSONString(boxItem, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return boxItemDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(BoxItem boxItem) {
        Map<String, Object> map = null;
        if (boxItem != null) {
            map = JSON.parseObject(JSON.toJSONString(boxItem, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return boxItemDAO.count(map);
    }

    /**
     * 查询BoxItem分页
     *
     * @param id
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<BoxItem>
     */
    @Override
    public List<BoxItem> list(Long id, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return boxItemDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return boxItemDAO.count(param);
    }

    /**
     * 根据箱体sn查询出箱体内所有产品信息
     *
     * @param sn sn
     * @return List<BoxItem>
     */
    @Override
    public List<BoxItem> getSampleByBoxSn(String sn) {
        if (StringUtils.isEmpty(sn)) {
            throw new BoxException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> params = new HashedMap();
        params.put("boxSn", sn);
        List<BoxItem> boxItemList = boxItemDAO.list(params);
        if (boxItemList != null && boxItemList.size() > 0) {
            String testUnitCode;
            TestUnit testUnit;
            String sampleCode;
            Sample sample;
            for (BoxItem boxItem : boxItemList) {
                testUnitCode = boxItem.getTestUnitCode();
                //查询出测试项
                testUnit = testUnitDAO.loadByCode(testUnitCode);
                if (testUnit != null) {
                    sampleCode = testUnit.getSampleCode();
                    //查询出样品
                    sample = sampleDAO.loadByCode(sampleCode);
                    if (sample != null) {
                        boxItem.setSampleName(sample.getName());
                    }
                }

            }
        }
        return boxItemList;
    }

}
